/*
 * 제조회사 신규 등록시 탭이 추가되도록 하는 자바 스크립트 파일
 */

function addTab(tabs) {
	var flag = true;
	$("#regCorp").click(function(e) {
		e.preventDefault();
		if(flag==true){
			flag = add(tabs);
		} else {
			tabs.tabs("option", "active", 1);
		}
		
	});
	
	// close icon: removing the tab on click
	tabs.delegate("span.ui-icon-close", "click", function() {
		var panelId = $(this).closest("li").remove().attr("aria-controls");
		$("#" + panelId).remove();
		flag = true;
		tabs.tabs("refresh");
	});
}
function add(tabs, flag) {
	var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";
	var label = "Corperation";
	var id = "corp";
	var li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(
			/#\{label\}/g, label));
	$.ajax({
		url : '/templateType/type4/addTab.do',
		type : "post",
		dataType : "html",
		success : function(data) {
			//				alert("hi");
			//				alert(data);
			tabs.find(".ui-tabs-nav").append(li);
			tabs.append("<div id='" + id + "'>" + data + "</div>");
			$(".ui-icon-close").css("float","left").css("margin","0.4em 0.2em 0 0").css("cursor","pointer");
			tabs.tabs("refresh");
			tabs.tabs("option", "active", 1);
			$(".btn").button();
			click();
			getLocList();
			
			return false;
		},
		error : function(jqXHR, textStatus, errorThrown) {
			alert(errorThrown);
			alert(jqXHR.status);
		}
	});
}

function getLocList() {
	var list = ["서울특별시","대구광역시","대전광역시","광주광역시","인천광역시","울산광역시","부산광역시",
	            "세종특별자치시","경기도","강원도","경상북도","경상남도","충청북도","충청남도",
	            "전라북도","전라남도","제주특별자치도"];
	$(list).each(function(i){
		if(i==0){
			$("#_tmp_address_list option:eq(0)").replaceWith("<option value=\'"+list[i]+"\'>"+list[i]+"</option>");
		} else{
			$("#_tmp_address_list").append("<option value=\'"+list[i]+"\'>"+list[i]+"</option>");
		}
		
	});
	$("#_tmp_address_list option:eq(0)").attr("seleted","selected");
	$("#cLoc").val($("#_tmp_address_list option:selected").attr("value"));
	
	$("#_tmp_address_list").change(function() {
		var str = $("#_tmp_address_list option:selected").attr("value");
		$("#cLoc").val(str);
	});
	
/*	우편번호 검색기능.. 미완성
	$("#search").click(function(e) {
		e.preventDefault();
		$('#zone_address_list').text("로딩중...");
		$.ajax({
			url : '/templateType/type4/goAddrSearch.do',
			type : "post",
			data : ({
				str : $('#str').val()
			}),
			dataType : "html",
			success : function(data) {
				$('#zone_address_list').html(data);
				$("div#zone_address_list").show();

				$("#_tmp_address_list").change(function() {
					var str = $("#_tmp_address_list option:selected").attr("value");
					var strArray = str.split(",");
					$("#cPost1").val(strArray[0]);
					$("#cPost2").val(strArray[1]);
					$("#cAddr1").val(strArray[2]);
					$("input[name=cPost1],input[name=cPost2],input[name=cAddr1]").attr("disabled",false);
					$("#cAddr2").val("상세주소를 입력하세요.").focus(function() {
						$("div#fo_insert").hide();
						$("div#zone_address_list").hide();
					});
				});
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert( errorThrown );
				alert( jqXHR.status );
				alert( jqXHR.statusText );
				alert( jqXHR.responseText );
				alert( jqXHR.readyState );
			}
		});
	});*/
}

